﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Xml;

    public class Utilities
    {

        public static List<XmlNode> GetTablesByHeader(XmlDocument document, string lookFor)
        {
            List<XmlNode> tables = new List<XmlNode>();
            var matchingHeaders = document.SelectNodes("//table/thead[contains(.,'" + lookFor + "')]");
            foreach (XmlNode header in matchingHeaders)
            {
                tables.Add(header.ParentNode);
            }
            return tables;
        }

        public static string GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)
        {
            string[] result = { string.Empty, string.Empty };
            int iIndexOfBegin = strSource.IndexOf(strBegin);

            if (iIndexOfBegin != -1)
            {
                if (includeBegin)
                    iIndexOfBegin -= strBegin.Length;

                strSource = strSource.Substring(iIndexOfBegin + strBegin.Length);

                int iEnd = strSource.IndexOf(strEnd);
                if (iEnd != -1)
                {
                    if (includeEnd)
                        iEnd += strEnd.Length;
                    result[0] = strSource.Substring(0, iEnd);
                    if (iEnd + strEnd.Length < strSource.Length)
                        result[1] = strSource.Substring(iEnd + strEnd.Length);
                }
            }
            else
                result[1] = strSource;
            return result[0];
        }

        public static string ScrapeHtml(string url)
        {
            string RawHtml;
            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                client.Encoding = new System.Text.UTF8Encoding();
                RawHtml = client.DownloadString(url);
            }
            RawHtml = RawHtml.Replace("&copy;", "");
            RawHtml = RawHtml.Replace("&middot;", "");
            RawHtml = RawHtml.Replace("document.write('", "");
            RawHtml = RawHtml.Replace("');", "");
            RawHtml = RawHtml.Replace("Ö", "&#214;");
            RawHtml = RawHtml.Replace("ö", "&#246;");
            RawHtml = RawHtml.Replace("Å", "&#197;");
            RawHtml = RawHtml.Replace("å", "&#229;");
            RawHtml = RawHtml.Replace("Ä", "&#196;");
            RawHtml = RawHtml.Replace("ä", "&#228;");
            RawHtml = RawHtml.Replace("& ", "&amp; ");
            return RawHtml;
        }

        public static void InsertToCache(string key, object value)
        {
            HttpContext.Current.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromHours(1));

        }

        public static object GetFromCache(string key)
        {
            return HttpContext.Current.Cache.Get(key);
        }

        public static string GetTableValue(IEnumerable<XElement> rows, string property)
        {
            string value;
            XElement propertyCell = rows.Descendants("td").Where(cell => cell.Value == property).FirstOrDefault();
            if (propertyCell != null)
            {
                var Row = propertyCell.Parent.Descendants("td").ElementAt(1);
                return Row.Value.Trim();
            }
            else
            {
                return string.Empty;
            }
        }


    }
